/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-28
 * Time: 15:26
 */
class Node{
    public int data;
    public Node next;

    public Node(int data){
        this.data = data;
    }
}

public class MyQueueLinked {
    private Node front;
    private Node rear;
    private int usedSize;

    //入队列
    public void offer(int val) {
        Node node = new Node(val);
        //第一个元素入队列
        if (this.front == null) {
            this.front = node;
            this.rear = node;
        }else {
            //先让前一个接上新数据
            this.rear.next = node;
            //再让尾节点指向新数据
            this.rear = node;
        }
        this.usedSize++;
    }

    //出队头元素
    public int poll() throws RuntimeException{
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int value = this.front.data;
        if (this.front.next == null) {
            this.front = null;
            this.rear = null;
        }else {
            this.front = this.front.next;
        }
        this.usedSize--;
        return value;
    }

    //得到对头元素，不删除
    public int peek() throws RuntimeException{
        if (isEmpty()) {
            throw new RuntimeException("队列空");
        }
        int value = this.front.data;
        return value;
    }

    public boolean isEmpty(){
        return !(this.usedSize > 0);
    }

    public int size(){
        return this.usedSize;
    }
}
